package com.zkthink.ceres.applet.ceresStore.controller;

import com.alibaba.fastjson.JSONObject;
import com.zkthink.base.R;
import com.zkthink.ceres.applet.ceresStore.domain.vo.*;
import com.zkthink.ceres.applet.ceresStore.service.CeresStoreService;
import com.zkthink.ceres.applet.order.domain.vo.OrderDetailVo;
import com.zkthink.ceres.common.utils.GaoDeMapUtil;
import com.zkthink.ceres.applet.pcVehicles.domain.vo.PcVehiclesServerVo;
import com.zkthink.ceres.applet.pcVehicles.service.PcVehiclesService;
import com.zkthink.clh.order.vo.MyqianbaoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author cmz
 * @Date 2024-04-30 15:40
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/applet/ceresStore")
@Api(value = "ceresStore", tags = "门店信息")
public class CeresStoreController {
    @Autowired
    private CeresStoreService ceresStoreService;

    @Autowired
    private PcVehiclesService pcVehiclesService;

    @ApiOperation(value = "查询附近可接订单门店、理疗车位置", notes = "查询附近可接订单门店、理疗车位置")
    @PostMapping(value = "/findNearbyStore")
    public R findNearbyStore(@RequestBody NearbyIdLocalVo nearbyIdLocalVo) {
        if (nearbyIdLocalVo.getServerChannel() == 0) {
            R nearbyStore = pcVehiclesService.findNearbyStore(nearbyIdLocalVo);
            List<PcVehiclesServerVo> pcVehiclesServerVo = (List<PcVehiclesServerVo>) nearbyStore.getData();
            List<StoreListVo> listVos = new ArrayList<>();
            for (PcVehiclesServerVo pv : pcVehiclesServerVo) {
                StoreListVo storeListVo = new StoreListVo();
                storeListVo.setId(pv.getId());
                storeListVo.setStoreName(pv.getLicensePlate());
                storeListVo.setDistance(pv.getDistance());
                JSONObject aMapByLngAndLat = GaoDeMapUtil.getAMapByLngAndLat(pv.getLongitude(), pv.getLatitude());
                String township = aMapByLngAndLat.getJSONObject("addressComponent").getString("township");
                String street = aMapByLngAndLat.getJSONObject("addressComponent").getJSONObject("streetNumber").getString("street");
                String number = aMapByLngAndLat.getJSONObject("addressComponent").getJSONObject("streetNumber").getString("number");
                storeListVo.setAddress(township + street + number);
                storeListVo.setProvince(aMapByLngAndLat.getJSONObject("addressComponent").getString("province"));
                storeListVo.setCity(aMapByLngAndLat.getJSONObject("addressComponent").getString("city"));
                storeListVo.setArea(aMapByLngAndLat.getJSONObject("addressComponent").getString("district"));
                storeListVo.setLongitude(pv.getLongitude());
                storeListVo.setLatitude(pv.getLatitude());
                listVos.add(storeListVo);
            }
            return R.success(listVos);
        }
        else if (nearbyIdLocalVo.getServerChannel() == 1) {
            R nearbyStore = ceresStoreService.findNearbyStore(nearbyIdLocalVo);
            List<AppletStoreServerVo> appletStoreServerVo = (List<AppletStoreServerVo>) nearbyStore.getData();
            List<StoreListVo> listVos = new ArrayList<>();
            for (AppletStoreServerVo pv : appletStoreServerVo) {
                StoreListVo storeListVo = new StoreListVo();
                storeListVo.setId(pv.getId());
                storeListVo.setDistance(pv.getDistance());
                storeListVo.setStoreName(pv.getStoreName());
                storeListVo.setAddress(pv.getAddress());
                storeListVo.setProvince(pv.getProvince());
                storeListVo.setCity(pv.getCity());
                storeListVo.setArea(pv.getArea());
                storeListVo.setLongitude(pv.getLongitude());
                storeListVo.setLatitude(pv.getLatitude());
                listVos.add(storeListVo);
            }
            return R.success(listVos);

        } else {
            return R.fail("参数错误");
        }
    }

    @ApiOperation(value = "查询附近可接产品订单门店、理疗车位置", notes = "查询附近可接产品订单门店、理疗车位置")
    @PostMapping(value = "/findNearbyStoreProduct")
    public R findNearbyStoreProduct(@RequestBody ProductNearbyIdLocalVo productNearbyIdLocalVo) {
        NearbyIdLocalVo nearbyIdLocalVo = new NearbyIdLocalVo();
        nearbyIdLocalVo.setOrderType(1);
        nearbyIdLocalVo.setLatitude(productNearbyIdLocalVo.getLatitude());
        nearbyIdLocalVo.setLongitude(productNearbyIdLocalVo.getLongitude());
        nearbyIdLocalVo.setServerId(productNearbyIdLocalVo.getServerId());
        nearbyIdLocalVo.setServerChannel(productNearbyIdLocalVo.getServerChannel()==1?0:1);//1理疗车对应0上门   2店铺对应1到店
        return this.findNearbyStore(nearbyIdLocalVo);
    }

    @ApiOperation("查询门店、车辆营业额")
    @PostMapping("/revenue")
    public R revenue( @RequestBody RevenuePageTypeDate pageTypeDate) {
        return ceresStoreService.revenue(pageTypeDate);
    }
    
}
